#!/bin/bash

source print-debug.sh
source style1.sh
source menu-callback-none-style.sh

CONFIG_EDIT="cat-edit"

function _file_exist() {
    test -e "$1"
    EXPECT_EQ_FN "$?" "0" "$1" "$2"
}

function _check_value() {
    IN_PATH="$1"
    IN_VALUE="$2"
    A=$(cat $IN_PATH)
    B="$IN_VALUE"
    EXPECT_EQ_FN "$A" "$B" "$3" "$4"
}
function TestCase_input() {
    _file_exist "/dev/input/event0" TestCase_input
    _file_exist "/dev/input/event1" TestCase_input
    _file_exist "/dev/input/event2" TestCase_input
    
    A=$(cat /sys/class/input/event0/device/name)
    B="20cc000.snvs:snvs-powerkey"
    EXPECT_EQ "$A" "$B"
    A=$(cat /sys/class/input/event2/device/name)
    B="iMX6UL Touchscreen Controller"
    EXPECT_EQ "$A" "$B"
    A=$(cat /sys/class/input/event1/device/name)
    B="gpio_keys"
    EXPECT_EQ "$A" "$B"
}

function TestCase_inputBatt() {
    _file_exist "/sys/class/gpio/pin_bat1_status/" TestCase_input
    _file_exist "/sys/class/gpio/pin_bat2_status/" TestCase_input
    _file_exist "/sys/class/gpio/pin_power_adapter/" TestCase_input

    _file_exist "/sys/class/gpio/pin_bat1_status/direction" TestCase_input
    _file_exist "/sys/class/gpio/pin_bat2_status/direction" TestCase_input
    _file_exist "/sys/class/gpio/pin_power_adapter/direction" TestCase_input
}

function _cat_interrupt() {
    echo "$T_LCD_BUFF" | grep "$1" > /dev/null
    A=$? ;B=0
    EXPECT_EQ "$A" "$B" "$1"
}

function TestCase_interrupt() {
     T_LCD_BUFF=$(cat /proc/interrupts)
    _cat_interrupt 'gpio-mxc  24 Edge      btn-1-24-down'
    _cat_interrupt 'gpio-mxc  24 Edge      btn-1-24-down'
    _cat_interrupt 'gpio-mxc  25 Edge      btn-1-25-left'
    _cat_interrupt 'gpio-mxc  26 Edge      btn-1-26'
    _cat_interrupt 'gpio-mxc  27 Edge      btn-1-27-right'
    _cat_interrupt 'gpio-mxc  28 Edge      btn-1-28-esc'
    _cat_interrupt 'gpio-mxc  29 Edge      btn-1-29-a-b'
    _cat_interrupt 'gpio-mxc  30 Edge      pwr-btn, btn-1-30-pwr'
    _cat_interrupt 'gpio-mxc  21 Edge      btn-3-21-enter'
    _cat_interrupt 'gpio-mxc  22 Edge      btn-3-22-rs'
    _cat_interrupt 'gpio-mxc  17 Edge      btn-4-17-home'
    _cat_interrupt 'gpio-mxc  18 Edge      btn-4-18-tab-shift'
}

function TestCase_lcd_bl() {
    _file_exist "/sys/class/backlight/mxs-bl/brightness" TestCase_lcd
    _file_exist "/sys/class/backlight/mxs-bl/timeout" TestCase_lcd
    _file_exist "/sys/class/backlight/mxs-bl/bl_power" TestCase_lcd
    _check_value "/sys/class/backlight/mxs-bl/bl_power" "0" "默认背光打开" TestCase_lcd
    # sleep 1
    echo 1 > /sys/class/backlight/mxs-bl/bl_power
    _check_value "/sys/class/backlight/mxs-bl/bl_power" "1" "背光关闭" TestCase_lcd
    # sleep 1
    echo 0 > /sys/class/backlight/mxs-bl/bl_power

    
    echo 0 >  /sys/class/backlight/mxs-bl/brightness
    EXPECT_EQ $? 0 "背光调节范围0"
    echo 100 >  /sys/class/backlight/mxs-bl/brightness
    EXPECT_EQ $? 0 "背光调节范围100"
    echo 101 >  /sys/class/backlight/mxs-bl/brightness
    EXPECT_EQ "$?" "1" "背光调节范围101"


    EXPECT_TODO 0 1  "TODO 电源键控制背光开关"
    EXPECT_TODO 0 1  "TODO 按键打开背光"
    EXPECT_TODO 0 1  "TODO 触摸屏打开背光"
}

function TestCase_lcd_disp() {
    echo 0 > /sys/class/backlight/mxs-bl/bl_power
    echo 100 >  /sys/class/backlight/mxs-bl/brightness
    ./lcdtest.elf rgb
    sleep 1
    EXPECT_EQ 1 1 LCD显示RGB
    ./lcdtest.elf area
    EXPECT_EQ 1 1 LCD显示范围
}

function TestCase_dts() {
    _check_value '/sys/firmware/devicetree/base/model' \
        'GLink TR600 Freescale i.MX6 ULL 14x14 EVK Board' \
        "DTS" \
        $FUNCNAME
}

function TestCase_i2c() {
    _file_exist "/dev/i2c-4" $FUNCNAME
    A=$(i2cdetect -y 4 | grep '50: -- UU -- -- -- -- -- -- -- -- -- -- -- -- -- --')
    EXPECT_EQ "$?" "0" I2C探测

    A=$(i2cget -f -y 4 0x51 0x02)
    sleep 2
    B=$(i2cget -f -y 4 0x51 0x02)
    EXPECT_NQ "$A" "$B" 时钟芯片运行

}

function show_log() {
    if [ "${1}" == "0" ]
    then
        echo
    else
        cat /tmp/a.txt
    fi
    
}
function TestCase_otdr() {
    /app/fpga-2016-chip.elf   -D /dev/spidev3.0       -t otdr  --speed 12000000 --peek 1000 --retry 2 --frame 2 > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" OTDR基本测试1S
    show_log $ret


    /app/fpga-2016-chip.elf   -D /dev/spidev3.0       -t otdr  --speed 12000000 --peek 20 --retry 2 --frame 2 > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" OTDR基本测试20ms观察
    show_log $ret
    
    /app/fpga-2016-chip.elf   -D /dev/spidev3.0  --laser 1310 -P 100km/160ns/n --add_ms 400 --power 4 --amp 0xb0  --apd 0x07   --peek 50 --speed 12000000 --retry 7 --frame 3 > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" OTDR基本测试50ms观察，重复7次
    show_log $ret

    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 --red 1 > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" 打开红光
    sleep 1
    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 --red s > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" 红光慢闪烁
    sleep 2
    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 --red f > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" 红光快速闪烁
    sleep 2
    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 --red 0 > /tmp/a.txt
    ret=$?
    EXPECT_EQ "$ret" "0" 关闭红光
    
}
function TestCase_APD_tempture() {
    export DBG_APD_TEMP=40
    /app/tr600-pp.elf > /tmp/a.txt
    A=$(cat /tmp/a.txt | grep 'APD temp')
    # EXPECT_EQ 'APD temp(℃ ) 40.00'  $A fff
    echo $A--------

    export DBG_APD_TEMP=35
    /app/tr600-pp.elf > /tmp/a.txt
    cat /tmp/a.txt | grep 'APD temp'
    export DBG_APD_TEMP=66
    /app/tr600-pp.elf > /tmp/a.txt
    cat /tmp/a.txt | grep 'APD temp'

    unset DBG_APD_TEMP
    /app/tr600-pp.elf > /tmp/a.txt
    cat /tmp/a.txt | grep 'APD temp'
}

function do_test_spi_speed() {
    unset SPEED

    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 1000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 1MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 5MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 10000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 10MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 12000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 12MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 16000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 16MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 20000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 20MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 24000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 24MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 50000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.0 50MHz"

    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 1000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 1MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 5MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 10000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 10MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 12000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 12MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 16000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 16MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 20000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 20MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 24000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 24MHz"
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 50000000 > /dev/null
    A=$(echo $?)
    EXPECT_EQ_LOG "$A" "0"  "spidev3.1 50MHz"
    read -p "按任意键退出"
}
function do_test_memleak() {
    # fpga-2016-chip.elf  -D /dev/spidev3.1  --forever 1  &
    # sleep 60
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --forever 1 > /tmp/a.txt &
    count=0
    while [ $count -lt 60 ]
    do
        let count++;
        sleep 1
        tail /tmp/a.txt
        free -m
        echo $i
    done
    kill $(pidof fpga-2016-chip.elf)
    echo "观察free是否明显变小"
    read -p "按任意键退出"
}
function do_test_fpga_redlaser() {
    echo 打开红光
    fpga-2016-chip.elf  -D /dev/spidev3.0 --red 1 > /dev/null
    sleep 2

    echo 关闭红光
    fpga-2016-chip.elf  -D /dev/spidev3.0 --red 0 > /dev/null
    sleep 2

    echo 红光慢闪烁
    fpga-2016-chip.elf  -D /dev/spidev3.0 --red s > /dev/null
    sleep 2

    echo 红光快闪烁
    fpga-2016-chip.elf  -D /dev/spidev3.0 --red f > /dev/null
    sleep 2

    fpga-2016-chip.elf  -D /dev/spidev3.0 --red 0 > /dev/null
    read -p "按任意键退出"
}

function do_test_2_fpga() {
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 3
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000  -t otdr --frame 3
    read -p "按任意键退出"
}
function do_test_fpga_otdr() {
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5 -d o
    read -p "按任意键退出"
}

function do_test_kill_fpga_2016_chip() {
    kill $(pidof fpga-2016-chip.elf)
}

function do_same_spi_fpga_otdr_mutex() {
    do_test_kill_fpga_2016_chip
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --forever 1 > /dev/null &
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5
    do_test_kill_fpga_2016_chip
    read -p "按任意键退出"
}

function do_same_spi_fpga_otdr_mutex_peek() {
    do_test_kill_fpga_2016_chip
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5 &
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --peek 200 --frame 5
    do_test_kill_fpga_2016_chip
    read -p "按任意键退出"
}


function do_same_spi_diff_cs_fpga_otdr_mutex() {
    do_test_kill_fpga_2016_chip
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --forever 1 > /dev/null &
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000  -t otdr --frame 5

    do_test_kill_fpga_2016_chip
    read -p "按任意键退出"
}

function do_diff_spi_otdr_mutex() {
    do_test_kill_fpga_2016_chip
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5 > /dev/null &
    sleep 1 # 稍微错开容易出问题
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000  -t otdr --frame 5 
    do_test_kill_fpga_2016_chip

    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000  -t otdr --frame 5 > /dev/null &
    sleep 1 # 稍微错开容易出问题
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5
    do_test_kill_fpga_2016_chip
    read -p "按任意键退出"
}


function do_diff_spi_otdr_mutex_peek() {
    do_test_kill_fpga_2016_chip
    unset SPEED
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5 > /dev/null &
    sleep 1 # 稍微错开容易出问题
    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000  -t otdr --peek 200 --frame 5 
    do_test_kill_fpga_2016_chip

    /app/fpga-2016-chip.elf -D /dev/spidev3.1 --speed 5000000  -t otdr --peek 200 --frame 5  > /dev/null &
    sleep 1 # 稍微错开容易出问题
    /app/fpga-2016-chip.elf -D /dev/spidev3.0 --speed 5000000  -t otdr --frame 5
    do_test_kill_fpga_2016_chip
    read -p "按任意键退出"
}

function do_1310_amp() {
    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 -t otdr --frame 1 --laser 1310 -P 180km/640ns/3min --apd 6 --amp 0x68
    echo "预览曲线：otdr.py /nfs/otdr-well.csv"
    read -p "按任意键退出"
}

function do_1550_amp() {
    # 改了硬件控制，amp传递0控制开关被关闭
    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 -t otdr --frame 1 --laser 1550 -P 180km/640ns/3min --apd 6 --amp 0x68
    echo "预览曲线：otdr.py /nfs/otdr-well.csv"
    read -p "按任意键退出"
}

function do_1550_amp_15s() {
    # 改了硬件控制，amp传递0控制开关被关闭
    /app/fpga-2016-chip.elf  -D /dev/spidev3.0 -t otdr --frame 1 --laser 1550 -P 180km/640ns/3min --apd 6 --amp 0x68 --add_ms 15000
    echo "预览曲线：otdr.py /nfs/otdr-well.csv"
    read -p "按任意键退出"
}

function menu_otdr() {
    local title_main="menu_otdr"
    local i=0
    local items_main[${i}]="Kill_SPI相关进程"           dos_main[${i}]="do_test_kill_fpga_2016_chip"  ; let i++
    local items_main[${i}]="spi速率测试"                dos_main[${i}]="do_test_spi_speed"  ; let i++
    local items_main[${i}]="红光测试"                   dos_main[${i}]="do_test_fpga_redlaser"  ; let i++
    local items_main[${i}]="是否存在fpga_open内存泄漏（60秒）"                  dos_main[${i}]="do_test_memleak"  ; let i++
    local items_main[${i}]="spidev3.0_OTDR（5次）"                                 dos_main[${i}]="do_test_fpga_otdr"  ; let i++
    local items_main[${i}]="spidev3.0_alive_+_spidev3.0_OTDR同时(5次）"                dos_main[${i}]="do_same_spi_fpga_otdr_mutex"  ; let i++
    local items_main[${i}]="spidev3.0_OTDR_+_spidev3.0_OTDR_peek_200同时(5次）"                dos_main[${i}]="do_same_spi_fpga_otdr_mutex_peek"  ; let i++
    local items_main[${i}]="spidev3.0_OTDR_+_spidev3.1_OTDR交替(3次)"                dos_main[${i}]="do_test_2_fpga"  ; let i++
    local items_main[${i}]="spidev3.0_alive_+_spidev3.1_OTDR同时(5次）"                dos_main[${i}]="do_same_spi_diff_cs_fpga_otdr_mutex"  ; let i++
    local items_main[${i}]="spidev3.0_OTDR_+_spidev3.1_OTDR同时(5次）"                dos_main[${i}]="do_diff_spi_otdr_mutex"  ; let i++
    local items_main[${i}]="spidev3.0_OTDR_+_spidev3.1_OTDR_peek_200同时(5次）"                dos_main[${i}]="do_diff_spi_otdr_mutex_peek"  ; let i++
    local items_main[${i}]="控制AMP_1310_100km"                dos_main[${i}]="do_1310_amp"  ; let i++
    local items_main[${i}]="控制AMP_1550_100km"                dos_main[${i}]="do_1550_amp"  ; let i++
    local items_main[${i}]="控制AMP_1550_100km_15秒"                dos_main[${i}]="do_1550_amp_15s"  ; let i++
    
    menu_select "${title_main}" ${#items_main[@]} "${items_main[*]}" "${dos_main[*]}"
}
menu_otdr

# mkdir output -p

# EXPECT_CLEAR
# # TestCase_dts
# # TestCase_input
# # TestCase_i2c
# # TestCase_inputBatt
# # TestCase_interrupt
# # TestCase_lcd_bl
# # TestCase_lcd_disp
# TestCase_APD_tempture
# TestCase_otdr

# echo $AB    
# EXPECT_REPORT
